package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.GoodsCategory;
import com.dingreading.cloud.admin.entity.table.GoodsCategoryTableDef;
import com.dingreading.cloud.admin.mapper.GoodsCategoryMapper;
import com.dingreading.cloud.admin.service.GoodsCategoryService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class GoodsCategoryServiceImpl extends BaseServiceImpl<GoodsCategoryMapper, GoodsCategory, GoodsCategoryTableDef> implements GoodsCategoryService {

    @Override
    protected GoodsCategoryTableDef getTable() {
        return GoodsCategoryTableDef.goodsCategory;
    }

    @Override
    public Page<GoodsCategory> pageList(PageUtil pageUtil, String name, Integer mallStatus, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(name))
            condition.and(table.name.like(name));
        if (enabled != null)
            condition.and(table.enabled.eq(enabled));
        if (mallStatus != null)
            condition.and(table.mallStatus.eq(mallStatus));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public void updateEnabled(List<Long> idList, Integer enabled) {
        UpdateChain.of(GoodsCategory.class)
                .set(table.enabled, enabled)
                .where(table.id.in(idList))
                .update();
    }

    @Override
    public boolean getSameNameCnt(Long id, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (id != null)
            condition.and(table.id.ne(id));
        condition.and(table.name.eq(name));

        return exists(condition);
    }

    @Override
    public int getMaxShowOrder() {
        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum));

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);

    }

    @Override
    public GoodsCategory getGtShowOrder(Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.gt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public GoodsCategory getLtShowOrder(Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.orderNum.lt(orderNum));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());
        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public void updateOrderNum(Long id, Integer orderNum) {
        UpdateChain.of(GoodsCategory.class)
                .set(table.orderNum, orderNum)
                .where(table.id.eq(id))
                .update();
    }

    @Override
    public List<GoodsCategory> getList() {
        QueryWrapper wrapper = QueryWrapper.create()
                .where(table.enabled.eq(1))
                .orderBy(table.orderNum.asc());
        return getMapper().selectListByQuery(wrapper);
    }

}
